package com.senlingspace.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.senlingspace.common.enums.LakalaTradeStateEnum;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.MoneyUtils;
import com.senlingspace.common.utils.PageUtils;
import com.senlingspace.system.domain.SlkjShiftManagement;
import com.senlingspace.system.domain.dto.GoodsIncomeDTO;
import com.senlingspace.system.domain.vo.GoodsIncomeVo;
import com.senlingspace.system.mapper.SlkjOrderMapper;
import com.senlingspace.system.service.SlkjGoodsIncomeService;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 *
 *
 * @author Administrator
 */
@Service
public class SlkjGoodsIncomeServiceImpl implements SlkjGoodsIncomeService {
    private static final Logger log = LoggerFactory.getLogger(SlkjGoodsIncomeServiceImpl.class);
    @Autowired
    private SlkjOrderMapper slkjOrderMapper;

    @Override
    public PageInfo<GoodsIncomeVo> selectGoodsIncomeList(Date startTime, Date endTime, Long shopId, Long productTpyeId) {
        if (startTime == null) {
            startTime = DateUtils.getStartOfDay(new Date());
        }else {
            startTime = DateUtils.getStartOfDay(startTime);
        }
        if (endTime == null) {
            endTime = DateUtils.getEndOfDay(new Date());
        } else {
            endTime = DateUtils.getEndOfDay(endTime);
        }
        Page<Object> pageExt = PageUtils.startPageExt(true);
        // 这是从数据库查询到的列表
        List<GoodsIncomeDTO> goodsIncomeDTOS = slkjOrderMapper.selectProductOrder(startTime, endTime, shopId, productTpyeId);
        long total = pageExt.getTotal();
        List<GoodsIncomeVo> goodsIncomeVos = new ArrayList<>();
        for (GoodsIncomeDTO goodsIncomeDTO : goodsIncomeDTOS) {
            GoodsIncomeVo goodsIncomeVo = new GoodsIncomeVo();
            goodsIncomeVo.setShopId(goodsIncomeDTO.getShopId());
            goodsIncomeVo.setShopName(goodsIncomeDTO.getShopName());
            goodsIncomeVo.setProductName(goodsIncomeDTO.getGoodsName());
            goodsIncomeVo.setProductImg(goodsIncomeDTO.getProductImg());
            goodsIncomeVo.setProductType(goodsIncomeDTO.getTypeName());
            goodsIncomeVo.setPrincipal(MoneyUtils.fen2Yuan(goodsIncomeDTO.getGoodCostPrice()));
            goodsIncomeVo.setPrice(MoneyUtils.fen2Yuan(goodsIncomeDTO.getGoodsPrice()));
            goodsIncomeVo.setSales(goodsIncomeDTO.getRefundTotalQuantity() + goodsIncomeDTO.getSuccessTotalQuantity());
            goodsIncomeVo.setTotalSales(MoneyUtils.fen2Yuan(goodsIncomeDTO.getRefundTotalAmount() + goodsIncomeDTO.getSuccessTotalAmount()));
            goodsIncomeVo.setRefund(MoneyUtils.fen2Yuan(goodsIncomeDTO.getRefundTotalAmount()));
            goodsIncomeVo.setRefundNum(goodsIncomeDTO.getRefundTotalQuantity());
            goodsIncomeVo.setGoodsId(goodsIncomeDTO.getGoodsId());
            goodsIncomeVos.add(goodsIncomeVo);
        }
        PageInfo<GoodsIncomeVo> pageInfo = new PageInfo<>(goodsIncomeVos);
        pageInfo.setList(goodsIncomeVos);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(pageExt.getPageNum());
        pageInfo.setPageSize(pageExt.getPageSize());
        return pageInfo;
    }

    @Override
    public List<GoodsIncomeVo> exportGoodsIncomeList(Date startTime, Date endTime, Long shopId, Long productTpyeId) {
        if(startTime == null){
            startTime = DateUtils.getStartOfDay(new Date());
        }else {
            startTime = DateUtils.getStartOfDay(startTime);
        }
        if (endTime == null) {
            endTime = DateUtils.getEndOfDay(new Date());
        } else {
            endTime = DateUtils.getEndOfDay(endTime);
        }
        // 这是从数据库查询到的列表
        List<GoodsIncomeDTO> goodsIncomeDTOS = slkjOrderMapper.selectProductOrder(startTime, endTime, shopId, productTpyeId);
        List<GoodsIncomeVo> goodsIncomeVos = new ArrayList<>();
        for (GoodsIncomeDTO goodsIncomeDTO : goodsIncomeDTOS) {
            GoodsIncomeVo goodsIncomeVo = new GoodsIncomeVo();
            goodsIncomeVo.setGoodsId(goodsIncomeDTO.getGoodsId());
            goodsIncomeVo.setShopId(goodsIncomeDTO.getShopId());
            goodsIncomeVo.setShopName(goodsIncomeDTO.getShopName());
            goodsIncomeVo.setProductName(goodsIncomeDTO.getGoodsName());
            goodsIncomeVo.setProductImg(goodsIncomeDTO.getProductImg());
            goodsIncomeVo.setProductType(goodsIncomeDTO.getTypeName());
            goodsIncomeVo.setPrincipal(MoneyUtils.fen2Yuan(goodsIncomeDTO.getGoodCostPrice()));
            goodsIncomeVo.setPrice(MoneyUtils.fen2Yuan(goodsIncomeDTO.getGoodsPrice()));
            goodsIncomeVo.setSales(goodsIncomeDTO.getRefundTotalQuantity() + goodsIncomeDTO.getSuccessTotalQuantity());
            goodsIncomeVo.setTotalSales(MoneyUtils.fen2Yuan(goodsIncomeDTO.getRefundTotalAmount() + goodsIncomeDTO.getSuccessTotalAmount()));
            goodsIncomeVo.setRefund(MoneyUtils.fen2Yuan(goodsIncomeDTO.getRefundTotalAmount()));
            goodsIncomeVo.setRefundNum(goodsIncomeDTO.getRefundTotalQuantity());
            goodsIncomeVos.add(goodsIncomeVo);
        }
        return goodsIncomeVos;
    }
}
